<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-button type="text" @click="handleDlg(0)">添加</el-button>
    </div>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          type="index"
          label="编号"
          width="180"
          align="center"
      >
      </el-table-column>
      <el-table-column
          prop="id"
          label="逻辑编号"
          width="180"
          align="center"
      >
      </el-table-column>
      <el-table-column
          prop="sourceName"
          label="领域名称"
          width="180" align="center">
      </el-table-column>
      <el-table-column
          prop="sort"
          label="排序" align="center">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间" align="center"
          :formatter="timeFormater"
      >
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template v-slot="scope">
          <el-button size="mini" @click="handleDlg(scope.row.id)">编辑</el-button>
          <el-button size="mini" @click="handleDeleteCategory(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <cu-resource ref="categoryDlg" @changeCategory="loadAllResourceList"/>
  </el-card>
</template>

<style scoped>

</style>


<script>
//资源领域列表
import {deleteByIdResource, getAllResourceList} from "@/api/resource/resource";
import CuResource from './CreateUpdateResource.vue'
import dayjs from 'dayjs'


export default {
  components: {
    CuResource: CuResource
  },
  created() {
    this.loadAllResourceList()
  },
  name: 'ResourceCategoryIndex',
  data() {
    return {
      resourcePageBo: {
        pageSize: 1,
        pageNum: 10,
        createTime: ''
      },
      resourceCategoryData: [],
      tableData: []
    }
  },
  methods: {
    loadAllResourceList() {
      getAllResourceList(this.resourcePageBo).then(res => {
        console.log("资源领域分页展示::::::::::", res);
        if (res.data.code === "200") {
          this.resourceCategoryData = res.data.data.rows;
          this.tableData = this.resourceCategoryData;
        } else {
          this.$message.error('加载资源类别信息失败!')
        }
        console.log(res)
      })
    },
    //时间格式化  这四个参数是默认的参数  行、列、值、索引
    timeFormater(row, column, value, index) {
      return dayjs(value).format('YYYY-MM-DD hh:mm:ss')
    },
    //组件调用子组件控制创建或编辑对话框
    handleDlg(categoryId = 0) {
      this.$refs.categoryDlg.showAndInit(categoryId)
    },
    //删除资源领域对象
    handleDeleteCategory(categoryId = 0) {
      this.$confirm('确认要删除这个资源名单吗?', '删除提示').then(
          () => {
            deleteByIdResource(categoryId).then(res=>{
              // debugger
              if(res.data.code=='200'){
                this.$message.success('删除资源名单成功!')
                this.loadAllResourceList()
              }
             else {
                this.$message.error('删除资源名单失败!')
              }
            })
          })
          .catch(e => {
            console.log("错误信息:", e);
          })
    }

  }
}

</script>
